using System;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;

namespace SuppKids.Core.Providers
{
	public class ApplicationSettingsProvider : IApplicationSettingsProvider
	{
		private readonly string licensesFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SuppKids", "Licenses");
		private readonly string databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SuppKids", "SuppKids.sdf");
		private readonly string personalInfoPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SuppKids", "PersonalInfo.xml");

		public ApplicationSettingsProvider()
		{
			var setting = ConfigFileSettings[Lambda.Property<IApplicationSettingsProvider>(x => x.LicensesFolder)];
			if (setting != null)
			{
				this.licensesFolder = Environment.ExpandEnvironmentVariables(setting);
			}

			setting = ConfigFileSettings[Lambda.Property<IApplicationSettingsProvider>(x => x.DatabasePath)];
			if (setting != null)
			{
				this.databasePath = Environment.ExpandEnvironmentVariables(setting);
			}

			setting = ConfigFileSettings[Lambda.Property<IApplicationSettingsProvider>(x => x.PersonalInfoPath)];
			if (setting != null)
			{
				this.personalInfoPath = Environment.ExpandEnvironmentVariables(setting);
			}
		}

		public string LicensesFolder
		{
			get { return licensesFolder; }
		}

		public string DatabasePath
		{
			get { return databasePath; }
		}

		public string PersonalInfoPath
		{
			get { return this.personalInfoPath; }
		}

		private static NameValueCollection ConfigFileSettings
		{
			get
			{
				return ConfigurationManager.AppSettings;
			}
		}
	}
}